﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;
using Grishko.DataTier;
using OutdoorLib.BaseEntity.Infrastructure;

namespace Grishko.UI
{
    public class MyListControl : Control, IListOfFilter
    {

        /// <summary>
        /// Собыие - щелчок по строчке
        /// </summary>
        public Func<string, bool> OnRowClick;
        

        public MyListControl() { }

        /// <summary>
        /// Разрисовка иконки перед строчкой
        /// </summary>
        public string ItemImgCss { get; set; }

        /// <summary>
        /// Css строчки
        /// </summary>
        public string ItemCss { get; set; }
        
        //Открыть закрыть отображение элемента
        private bool bEnableRander = false;

        protected override void OnLoad(EventArgs e)
        {
            if (this.Page.Request["__EVENTTARGET"] == this .ClientID)
            {
                if (null != OnRowClick)
                    OnRowClick(this.Page.Request["__EVENTARGUMENT"]);
            }       
        }

        protected override void OnDataBinding(EventArgs e)
        {
            //base.OnDataBinding(e);
            bEnableRander = true;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                writer.Write("Таблица ");
                return;
            }

            //// прорисовываем только если есть источник
            if (null == DataSource)
                return;
            if (bEnableRander)
                MyRender(writer);

        }

        private void MyRender(HtmlTextWriter writer)
        {
            if (null == DataSource())
                            return ;

            List <EntitySingle > l = DataSource().GetCollection();
            
            Table tbl = new Table() { Width = new Unit("100%") };
            
            foreach (EntitySingle ent in l)
            {
                // первая клетка
                Panel pnl = new Panel() { Width = new Unit("400px") };
                TableCell cell = new TableCell();
                cell.Controls.Add(pnl);

                cell.Width = new Unit("400px");
                TableRow rw = new TableRow();
                rw.Attributes.Add("onClick", GetOnClickCmd(ent.ID));
                //cell.Text = ent.ID;
                cell.CssClass = "Contract_Cell";                
                rw.Cells.Add(cell);
                
                //Вторая клетка
                Panel pnl1 = new Panel() { Width = new Unit("100%"),ID ="RowBox" };
                TableCell cell1 = new TableCell();
                cell1.Controls.Add(pnl1);
                cell1.CssClass = "Contract_Cell";
                pnl1.Controls.Add(GetTable());
                rw.Cells.Add(cell1);



                

                tbl.Rows.Add(rw);
            }
            tbl.RenderControl(writer);
        }

        private Table GetTable()
        {
            Table tbl = new Table() { Width = new Unit("100%"), CellPadding=1, CellSpacing =2 };
            TableRow rw = new TableRow();
            for (int i = 10; i < 11; i++)
            {
                TableCell NewCell = new TableCell();
                //NewCell.Text = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
                NewCell.Text = "&nbsp&nbsp&nbsp";
               // NewCell.Width = new Unit("300px");
                
                NewCell.CssClass = "Vacant_Cell";
                rw.Cells.Add(NewCell);
            }

            tbl.Rows .Add (rw);
            return tbl;

        }
        private string GetOnClickCmd(string RowId)
        { 
            string [] s={this.ClientID ,RowId};
            return string.Format ("CallServer('{0}', '{1}');",s);
        }


        #region Члены IListOfFilter

        public Func<Filter> DataSource{get;set;}        

        #endregion
    }
    
}
